<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CurdDemoController extends Controller
{
    protected array $form_data;

    public function __construct()
    {
        $this->form_data['table_config'] = [
            'stripe' => true,
            'height' => '100%',
            'selection' => true,
        ];
    }

    public function table(int $current_page): array
    {
        $this->form_data = array_merge($this->form_data, DB::table('region_city')->paginate($current_page)->toArray());
        $field = array_keys((array)$this->form_data['data'][0]);
        $fields = [];
        $columns = getColumns('region_city');
        foreach ($field as $value) {
            $f = arraySearch($columns, 'Field', $value);
            $fields[] = [
                'label' => $value,
                'prop' => reset($f)->Comment ?: reset($f)->Field
            ];
        }
        $this->form_data['table_fields'] = $fields;
        return outPut($this->form_data);
    }
}
